package com.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.api.ItemService;
import com.dto.ItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: sksun2
 * @create: 2023-02-10
 */
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @GetMapping("{id}")
    @SentinelResource(value = "item",blockHandlerClass = ItemController.class,blockHandler = "helloBlock",fallback="失败")
    public ItemDTO getItemById(@PathVariable Long id){
        return itemService.getItemById(id);
    }

    /**
     * 处理异常的方法必须是静态方法添加static, 并需要添加sentinel的异常参数BlockException
     */
    public static String helloBlock(BlockException e) {
        System.out.println("您已被限流");
        return "您已被限流";
    }


}
